package com.cx.core.magazine.util;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.nio.channels.FileChannel;
import java.util.Calendar;
/**
 * 
 * @author li.chh
 *
 */
public class MagTool {
	private static MagTool magTool = null;

	private MagTool() {

	}

	public static MagTool getMagTool() {
		if (magTool == null) {
			magTool = new MagTool();
		}
		return magTool;
	}
	/**
	 * 将文件保存到缓冲位置
	 * @param filename 文件名称
	 * @param ins 输入流
	 * @param flashflag 是否刷新文件内容
	 * @return
	 * @throws Exception
	 */
	public String saveFileTolocal(String filename, InputStream ins,
			boolean flashflag) throws Exception {
		String filepath = null;
		String path = MagParm.IMG_CACHE_PATH + filename;
		String returnpath = "resource/cache/images/"+filename;
		System.out.println(path);
		File file = new File(path);
		if (file.exists() && flashflag == false) {
			return path;
		}
		System.out.println(file.getAbsolutePath());
		// file.mkdirs();
		if (!file.canWrite()) {
			file.setWritable(true);
		}

		file.createNewFile();
		FileOutputStream fo = new FileOutputStream(file);
	//	FileChannel fco = fo.getChannel()
		//FileChannel fio = ins.get		
		byte[] buffer = new byte[1024];
		while (ins.read(buffer) != -1) {
			fo.write(buffer);
		}
		fo.flush();
		fo.close();
		ins.close();
		return filename;
	}
	
}
